package com.wzcl.app.dao.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.wzcl.app.dao.model.common.Base;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Data
@Entity
@Table(name = "orders",
        uniqueConstraints = {
        @UniqueConstraint(columnNames = {"no"})
        },
        indexes = {
             @Index(columnList = "userPhone")
        }
)
@EntityListeners(AuditingEntityListener.class)
@ApiModel(value = "订单表")
public class Orders implements Base {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ApiModelProperty(value = "订单编号")
    private String no;

    @ApiModelProperty(value = "用户Id")
    private Integer userId;

    @ApiModelProperty(value = "用户手机")
    private String userPhone;

    @ApiModelProperty(value = "创建时间")
    @CreatedDate
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @ApiModelProperty(value = "修改时间")
    @LastModifiedDate
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    @ApiModelProperty(value = "状态")
    private Integer state;

    @ApiModelProperty(value = "支付状态")
    private Integer payState;

    @ApiModelProperty(value = "订单价格(原价)")
    private BigDecimal money;

    @ApiModelProperty(value = "商品价格（现价）")
    private BigDecimal productMoney;

    @ApiModelProperty(value = "实际付款金额")
    private BigDecimal payMoney;

    @ApiModelProperty(value = "订单状态")
    private Integer ordersState;

    @ApiModelProperty(value = "快递状态")
    private Integer deliveryState;

    @ApiModelProperty(value = "是否需要快递货物")
    private Boolean needDelivery;

    @ApiModelProperty(value = "生成订单的接口标记")
    private String createFrom;

    @ApiModelProperty(value = "系统类型")
    private Integer sysType;

    @ApiModelProperty(value = "用户姓名")
    @Transient
    private String userName;

    @JSONField(serialize = false)
    @ManyToOne
    @ApiModelProperty(value = "配送信息")
    @JoinColumn(name = "sendPath")
    private SendPath sendPath;

    @JSONField(serialize = false)
    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @ApiModelProperty(value = "订单详情")
    @JoinTable(name = "orderAndDetails",
            joinColumns = {@JoinColumn(name = "ordersId",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "orderDetailId",referencedColumnName = "id")})
    private List<OrderDetail> details;

    @Override
    public String toString() {
        return "Orders{" +
                "id=" + id +
                ", no='" + no + '\'' +
                ", userId=" + userId +
                ", userPhone='" + userPhone + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", state=" + state +
                ", payState=" + payState +
                ", money=" + money +
                ", productMoney=" + productMoney +
                ", payMoney=" + payMoney +
                ", ordersState=" + ordersState +
                ", deliveryState=" + deliveryState +
                ", needDelivery=" + needDelivery +
                ", createFrom='" + createFrom + '\'' +
                ", sendPath=" + sendPath +
                ", details=" + details +
                ", sysType=" + sysType +
                ", userName=" + userName +
                '}';
    }
}